<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_Utility_Widgets_Dictionary_Widget extends Gio_Core_Widget
{
	public function showAction()
	{
		$this->view->unid = uniqid('utilityDictionary_');
		$this->view->module = $this->getParam('module');
		$this->view->widget = $this->getParam('widget');
	}
	
	public function translateAction()
	{
		$text = $this->getParam('text');
		$from = $this->getParam('from'); 
		$to	  = $this->getParam('to');	
		
		$config = Gio_Core_Config_Xml::getConfig();
		/**
		 * Get translator service
		 */
		$service = (string)$config->localization->translate->service->using;
		switch ($service) {
			/**
			 * Use MicrosoftTranslator service
		 	 * - Get supported languages: 
		 	 * http://api.microsofttranslator.com/V1/Http.svc/GetLanguages?appId=xxx
		 	 * @see http://msdn.microsoft.com/en-us/library/ff512399.aspx
		 	 * 
		 	 * - Translate:
		 	 * http://api.microsofttranslator.com/V2/Ajax.svc/Translate?appId=xxx&text=xxx&from=en&to=fr
		 	 * @see http://msdn.microsoft.com/en-us/library/ff512406.aspx
			 */
			case 'microsoft':
				$url    = 'http://api.microsofttranslator.com/V2/Ajax.svc/Translate';
				$params = array(
					'text' => htmlentities($text),
					'from' => $from,
					'to'   => $to,
				);
				if (isset($config->localization->translate->service->microsoft->apikey)) {
					$params['appId'] = $config->localization->translate->service->microsoft->apikey;
				}
				
				$content = file_get_contents($url . '?' . http_build_query($params));
				if (strpos($content, 'ArgumentException') === false) {
					$content = ltrim($content, '"');
					$content = rtrim($content, '"');
					
					return $content;
				} else {
					/**
					 * The target language is not supported or the API key is not valid
					 */
					return null;					
				}
				break;
			
			/**
			 * Use Google translator service
			 * @see http://code.google.com/apis/ajaxlanguage/documentation/reference.html#_intro_fonje
			 */
			case 'google':
			default:
				/**
				 * The alternative way: Use Zend_Http_Client
				 * <code>
				 * 	$client = new Zend_Http_Client();
				 * 	$client->setConfig(array('timeout' => 30))
				 * 			->setUri($url)
				 * 			->setMethod('GET')
				 * 			->setParameterGet('v', '1.0')
				 * 			->setParameterGet('q', $text)
				 * 			->setParameterGet('langpair', $source[0] . '|' . $target[0]);
				 *	$content = $client->request()->getBody();
				 * </code>
				 */
				$url    = 'http://ajax.googleapis.com/ajax/services/language/translate';
				$params = array(
					'v' 	   => '1.0',
					'q' 	   => $text,
					'langpair' => $from . '|' . $to,
				);
				if (isset($config->localization->translate->service->google->apikey)) {
					$params['key'] = $config->localization->translate->service->google->apikey;
				}
				$content = file_get_contents($url . '?' . http_build_query($params));
				$json = new Services_JSON();
				$result  = $json->decode($content);
				$this->view->result = $result->responseData->translatedText;
				break;
		}
	}
	
	public function configAction()
	{
		$this->view->moduleName = Gio_Core_Widget::getIntance()->getModuleName();
		$this->view->widgetName = Gio_Core_Widget::getIntance()->getWidgetName();
		$this->view->widgetIndex = $this->getParam('widget_index');
	}
}